﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Pasteles
{
    public partial class LoginForm : Form
    {
        public static string manv = "";
        public static string sShopID = "";
        public static string sStartTime = "";
        public static DateTime startShiftDateTime = DateTime.Now;

        public LoginForm()
        {
            InitializeComponent();
            sStartTime = startShiftDateTime.ToLongTimeString();
            StartTimeTB.Text = sStartTime;
        }

        private void onExitBtnClicked(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LoginBtnClicked(object sender, EventArgs e)
        {
            string sRole = cbSeekWork.Text;
            if (sRole.Equals("Admin"))
            {
                AdminForm adminForm = new AdminForm();
                adminForm.Activate();
                adminForm.Show();
            }
            else if (sRole.Equals("Cashier"))
                try
                {   
                    manv = txtID.Text;
                    sShopID = shopIDcmb.Text;
                    this.Hide();
                    this.OpenForm();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cashier error: " + ex.Message);
                }
        }
          
        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
        private void OpenForm()
        {
            CustomerIDfromMySQLTB cashierForm = new CustomerIDfromMySQLTB();
            cashierForm.Activate();
            cashierForm.Show();
        }
        private void SettingBtn_Click(object sender, EventArgs e)
        {
            SettingsForm frm = new SettingsForm();
            frm.ShowDialog(); 

        }


        private void checkWorkingShift(DateTime currentTime){
            XmlReader reader = XmlReader.Create("shift.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                    && reader.Name == "Shift")
                {
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "Morning")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.Name == "begin")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Morning begin = " + reader.Value);
                                            //sConfig_line1 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if

                                if (reader.Name == "end")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Morning end = " + reader.Value);
                                            //sConfig_line2 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if                                
                            }
                            reader.Read(); // delete it ?
                        } //end if if (reader.Name == "Morning")
                      
                        if (reader.Name == "Afternoon")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.Name == "begin")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Afternoon begin = " + reader.Value);
                                            //sConfig_line1 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if

                                if (reader.Name == "end")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Afternoon end = " + reader.Value);
                                            //sConfig_line2 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if                                
                            }
                            reader.Read(); // delete it ?
                        } //end if if (reader.Name == "Afternoon")
                 
                        if (reader.Name == "Evening")
                        {
                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.Name == "begin")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Evening begin = " + reader.Value);
                                            //sConfig_line1 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if

                                if (reader.Name == "end")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            MessageBox.Show("Evening end = " + reader.Value);
                                            //sConfig_line2 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if                                
                            }
                            reader.Read(); // delete it ?
                        } //end if if (reader.Name == "Evening")

                    } //end while
                } //end if
            } //end while
            reader.Close();
        }// 
        public void saveLoginTime(string currentDateTime, string endDateTime)
        {
            string newValue = string.Empty;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("shift.xml");
                XmlNode node1 = xmlDoc.SelectSingleNode("Shift/Morning");
                node1.Attributes[0].Value = currentDateTime; // begin 
                node1.Attributes[1].Value = endDateTime; // end

                XmlNode node2    = xmlDoc.SelectSingleNode("Shift/Morning/begin");
                node2.InnerText  = "";
                XmlNode node21   = xmlDoc.SelectSingleNode("Shift/Morning/end");
                node21.InnerText = "";
                xmlDoc.Save("shift.xml");
            }
            catch (Exception error)
            {
                MessageBox.Show("Saving XML config file error: " + error.ToString());
            }

        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void StartTimeTB_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
